/**
 * The MIT License
 *
 * Copyright (c) 2010 Benjamin Klum
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.helgoboss.obr_facade_ref_impl

import org.helgoboss.obr_facade.{Resource, ResourceId, ResourceState}
import org.osgi.service.obr.{Resource => ObrResource}
import org.osgi.framework.Bundle


class ResourceImpl(val obrResource: Option[ObrResource], val bundle: Option[Bundle]) extends Resource {
    lazy val id = ResourceId(symbolicName, version.toString)
    
    lazy val symbolicName = {
        obrResource match {
            case Some(r) => r.getSymbolicName
            case None => bundle.get.getSymbolicName
        }
    }
    
    lazy val version = {
        obrResource match {
            case Some(r) => r.getVersion
            case None => bundle.get.getVersion
        }
    }
    
    lazy val categories = {
        obrResource match {
            case Some(r) => r.getCategories.toList
            case None => 
                val categoriesString = bundle.get.getHeaders.get("Bundle-Category").asInstanceOf[String]
                if (categoriesString == null) {
                    Nil
                } else {
                    categoriesString.split(",").map(_.trim).toList
                }
        }
    }
    
    lazy val presentationName = {
        obrResource match {
            case Some(r) => r.getPresentationName
            case None => 
                val bundleName = bundle.get.getHeaders.get("Bundle-Name").asInstanceOf[String]
                if (bundleName == null) {
                    ""
                } else {
                    bundleName
                }
        }
    }
    
    lazy val state = {
        import Bundle._
        import ResourceState._
        bundle match {
            case Some(b) =>
                b.getState match {
                    case UNINSTALLED => Uninstalled
                    case INSTALLED => Installed
                    case RESOLVED => Resolved
                    case STARTING => Starting
                    case STOPPING => Stopping
                    case ACTIVE => Active
                }
                
            case None => NotInstalled
        }
    }

    override def equals(o: Any) = {
        if (o.isInstanceOf[Resource]) {
            val otherResource = o.asInstanceOf[Resource]
            symbolicName == otherResource.symbolicName && version == otherResource.version
        } else {
            false
        }
    }
    
    override def hashCode = {
        var i = 7
        i = 31 * i + symbolicName.hashCode
        i = 31 * i + version.hashCode
        i
    }
}